package br.senai.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.senai.entity.Usuario;
import br.senai.util.Conexao;

public class LoginDAO {
	Connection connection;
	
	public LoginDAO(){
		connection = Conexao.getConnection();
	}
	
	public Boolean validaLogin(String login, String senha){
		
		Boolean usuarioLogado = false;
		PreparedStatement stmt;
		ResultSet rs;
		try {
			stmt = connection.prepareStatement("select * from usuario where login=? and senha=?");
			stmt.setString(1, login);
			stmt.setString(2, senha);
			rs = stmt.executeQuery();
			while(rs.next()){
				usuarioLogado = true;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.getMessage();
		}
		return usuarioLogado;
	}
	
	public List<Usuario> listarUsuarios(){
		List<Usuario> listaUsuarios = new ArrayList<Usuario>();
		Usuario u = new Usuario();
		PreparedStatement stmt;
		ResultSet rs;
		try {
			stmt = connection.prepareStatement("select * from usuario");
			rs = stmt.executeQuery();
			while(rs.next()){
				u.setLogin(rs.getString("login"));
				u.setSenha(rs.getString("senha"));
				listaUsuarios.add(u);
			}
			rs.close();
			stmt.close();
			
		} catch (Exception e) {
			e.getMessage();
		}
		
		return listaUsuarios;
	}

}
